package com.qf.kdxf.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class SpeechExUtil {
    // webapi接口地址
    private static final String WEBSING_URL = "http://webqbh.xfyun.cn/v1/service/v1/qbh";
    // 应用ID
    private static final String APPID = "ced63297";
    // 接口密钥
    private static final String API_KEY = "fa3ef253b402aedd87a92676047e5010";

    private static final String ENGINE_TYPE = "afs";


    private static final String AUE = "raw";

    /*客户端传输一个audio_url参数时，Http Request Body需为空*/
    // private static final String AUDIO_URL = "http://cdn.inyuapp.com/audios/20180524120030bEAwdc.wav";
//    private static final String AUDIO_PATH ="D:\\QianFeng\\案例研发\\kdxf\\src\\main\\resources\\static\\一次就好16k.wav";
    /**
     * 合成 WebAPI 调用示例程序
     *
     * @param args
     * @throws IOException
     */
//    public static void main(String[] args) throws IOException {
//        Map<String, String> header = buildHttpHeader();
//
//        byte[] audioByteArray = FileUtil.read(AUDIO_PATH);
//        String result = HttpUtil.doPost(WEBSING_URL, header, audioByteArray);
//        System.out.println("WebAPI 接口调用结果：" + result);
//    }

    /**
     * 组装http请求头
     */
    public static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {
        String curTime = System.currentTimeMillis() / 1000L + "";
        // 如果使用audio_url传输音频，须在param中添加audio_url参数
        String param = "{\"aue\":\"" + AUE + "\",\"engine_type\":\"" + ENGINE_TYPE + "\"}";
        String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
        String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
        Map<String, String> header = new HashMap<String, String>();
        header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        header.put("X-Param", paramBase64);
        header.put("X-CurTime", curTime);
        header.put("X-CheckSum", checkSum);
        header.put("X-Appid", APPID);
        return header;
    }





}
